/*
visqrt
Copyright (C) 2010  Bhupendra Aole

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/
/*
 * File: STDPlugin.java
 * Author: Bhupendra Aole
 * Date: Aug 31, 2010: 
 * 
 * Revisions:
 * ----------
 */

package aole.plugin;

import java.lang.reflect.Method;
import java.util.HashMap;

import aole.object.CameraPinhole;
import aole.object.Renderable;

/**
 * 
 */
public class STDPlugin implements VQPlugin {
	private static final HashMap<String, Class<? extends VQObject>> objects = new HashMap<String, Class<? extends VQObject>>() {
		{
			put("sphere", aole.object.Sphere.class);
			put("plane", aole.object.Plane.class);
			put("disk", aole.object.Disk.class);
			put("light", aole.object.PointLight.class);
			put("camera", aole.object.CameraPinhole.class);
		}
	};
	// TODO: think of some better architecture
	private static final String keywords[] = { "camera", "sphere", "plane",
			"disk", "light", };
	private VQScene scene;
	// the only camera object
	private CameraPinhole cam = new CameraPinhole();

	/* (non-Javadoc)
	 * @see aole.plugin.VQPlugin#create(java.lang.String)
	 */
	public VQObject create (String object, String name) {
		VQObject s = null;
		Class<? extends VQObject> c = objects.get(object);
		if (c != null) {
			try {
				s = c.newInstance();
				if (s instanceof Renderable)
					scene.addRenderable((Renderable) s);
				else if (s instanceof VQLight)
					scene.addLight((VQLight) s);
				else if (s instanceof VQCamera)
					s = cam;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return s;
	}

	/* (non-Javadoc)
	 * @see aole.plugin.VQPlugin#getKeywords()
	 */
	public String[] getKeywords () {
		return keywords;
	}

	/* (non-Javadoc)
	 * @see aole.plugin.VQPlugin#init(aole.plugin.VQScene)
	 */
	public void init (VQScene scene) {
		this.scene = scene;
		scene.setCamera(cam);
	}

	/* (non-Javadoc)
	 * @see aole.plugin.VQPlugin#setProperty(aole.plugin.VQObject, java.lang.String, java.lang.String)
	 */
	public void setProperty (VQObject obj, String prop, String value) {
		Method m;
		try {
			m = obj.getClass().getMethod("read" + prop.toLowerCase(),
					String.class);
			m.invoke(obj, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
